package com.liwenfeng.tcpandupd;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class TcpClient {
    public static void main(String[] args) {
        try (
                //1.创建Socket对象，并同时请求与服务端程序的连接。
                Socket socket = new Socket("127.0.0.1", 8888);
                //2.从socket通信管道中得到一个字节输出流，用来发数据给服务端程序。
                OutputStream os = socket.getOutputStream();
                //3.把低级的字节输出流包装成数据输出流
                DataOutputStream dos = new DataOutputStream(os);
        ) {
            Scanner sc = new Scanner(System.in);

            while (true) {
                System.out.println("请输入：");
                String msg = sc.nextLine();

                if (msg.equals("exit")) break;

                //4.开始写数据出去
                dos.writeUTF(msg);
                dos.flush();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
